package Middlewares

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

// Cors 跨域解决
func Cors() gin.HandlerFunc {
	return func(c *gin.Context) {

		method := c.Request.Method
		c.Header("Access-Control-Allow-Origin", "*")                                  // 接收所有请求
		c.Header("Access-Control-Allow-Methods", "POST,GET,DELETE,OPTIONS,PUT,PATCH") // 设置可使用的请求方法
		c.Header("Access-Control-Expos-Headers", "Content-Length,Content-Type ,Access-Control-Allow-Origin,Access-Control-Allow-Headers")
		c.Header("Access-Control-Allow-Headers", "Content-Type ,AccessToken,Authorization,Token,X-Token") // 可设置的头部key
		c.Header("Access-Control-Allow-Credentials", "true")

		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusOK)
		}
		c.Next()
	}
}
